home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-12-01 | 12.1 KB | 571 lines | [TEXT/CWIE] |
- // NewDynamic
- // version 1.0.1
- // translated to THINK C by Ken Long <kenlong@netcom.com>
- // ported to CW7 on 951201
-
- //• Another old, dead source code brought back to life by
- //• Kenneth A. Long, at itty bitty bytes(tm)!
-
- //• Originally a MacPascal source, from Northwestern University,
- //• from March 22, 1985! I translated it to Think C and perked it
- //• up a little.
-
- //• I was interested in the drawing - not the console crap, so I
- //• got rid of all of it. It should be replaced by a dialog or some
- //• on-screen controls.
-
- //• I added some colorization and a vertical line drawing routine. I
- //• also added a grow box and handler, but the window doesn't update.
-
- //• Other than that it's pretty much the same.
-
- //• I don't know what sort of a tangent the shrinks were on that had
- //• something to do with the supposed purpose of these graphs, but it
- //• has some use as a value graphing thing - if you know what to
- //• change and where. Biorythm?
-
- //• Enjoy!
-
- //• kenlong@netcom.com
-
- #include <stdio.h>
- #include <packages.h>
- #include <math.h>
-
- #define windowID 256
- #define lastMenu 3 /* number of menus */
- #define appleMenu 1 /* menu ID for desk accessory menu */
- #define fileMenu 256 /* menu ID for File menu */
- #define editMenu 257 /* menu ID for Edit menu */
-
- //#define maxValue 512
- #define maxVectSize 8
- short scale_V = 1;
- #define scale_H 1
-
- typedef double matrixType[maxVectSize][maxVectSize];
- typedef double vectorType[maxVectSize];
- typedef char titletype[21];
-
- Boolean quit = false;
-
- Rect Trect, dragRect, growIconRect;
- EventRecord myEvent;
- WindowPtr myWindow, whichWindow;
- WindowRecord wRecord;
- short code;
- MenuHandle myMenus[lastMenu]; /*Handles to all of the menus*/
- DialogPtr theDialog;
-
- vectorType need, deltaNeed, response, deltaResponse, alphaVector,
- stimulation, consumVector, tempVct1, tempVct2;
- matrixType generalization, consumation, excitation, inhibition,
- tempmatrix, ident;
- short vectSize, loop, stimuli;
- double alpha, personality;
- char answer;
-
- //•
- void SetForeColor (short red, short green, short blue);
- void SetUpMenus (void);
- void Startup (void);
- void MatrixVector_X (double *a, double (*b)[maxVectSize], double *c, short n,
- short m);
- void AddVectors (double *a, double *b, double *c, short n);
- void SubtractVectors (double *a, double *b, double *c, short n);
- void ZeroVector (double *a, short n);
- void VectorInit (double *a, short n);
- void DrawVectors (double *a, double *b, short n, short loop, short offset,
- short scale_V);
- void Vector_X_Scalar (double *a, double s, double *b, short n);
- void Matrix_X_Scalar (double (*a)[maxVectSize], double s,
- double (*b)[maxVectSize], short n);
- void Identity (double (*temp)[maxVectSize], short vectSize);
- void DynamicSimulation (void);
- void Message (short dialogNumber);
- void DrawTimeLines (void);
- void FakeGrowIcon (WindowPtr wPtr, Boolean visible);
- void MyGrowWindow (WindowPtr w, Point p);
- short DoCommand (long mResult);
- short HandleMouseDown (short windowPart, WindowPtr whichWindow,
- EventRecord *myEvent);
- void WaitForCommand (void);
- Boolean ColorCheck (void);
- void main (void);
- //•
- void SetForeColor(short red, short green, short blue)
- {
- RGBColor hue;
-
- hue.red = red;
- hue.green = green;
- hue.blue = blue;
-
- RGBForeColor (&hue);
- }
-
- void SetUpMenus(void)
- {
- short i;
-
- InitMenus ();
- myMenus[0] = GetMenu(appleMenu);
- AddResMenu(myMenus[0], 'DRVR'); /*add desk accessories*/
- myMenus[1] = GetMenu(fileMenu);
- myMenus[2] = GetMenu(editMenu); /*not really used, but just for practice*/
- for (i = 0; i <= lastMenu - 1; i++)
- InsertMenu(myMenus[i], 0);
- DrawMenuBar ();
- }
-
- void Startup(void)
- {
- InitGraf(&qd.thePort); /*start initialization sequence*/
- InitFonts ();
- InitWindows ();
- TEInit ();
- InitDialogs(0L);
- InitCursor (); /*make cursor an arrow*/
- FlushEvents(everyEvent, 0);
-
- /*Writelns and Readlns text will go to my window*/
-
- /* WWINIT;*/
- /*initialize the WritelnWindow unit*/
- /*not used anymore*/
- /*with Trect do*/
- /*begin*/
- /*top := 40;*/
- /*right := 480;*/
- /*bottom := 330;*/
- /*left := 40;*/
- /*end;*/
- /*GetFNum('Geneva', familyID);*/
- /*WWNew(Trect, 'Animals', false, true, 80, FamilyID, 9);*/
- }
-
- void MatrixVector_X(double *a, double (*b)[maxVectSize], double *c,
- short n, short m)
- {
- /*A:= B C A is a m dimensional vector*/
- /*B is a m x n dimensional matrix*/
- /*C is a n dimensional vector*/
- short i, j;
- double aij;
-
- for (i = 0; i <= n - 1; i++)
- { /*rows*/
- aij = 0.0;
- for (j = 0; j <= m - 1; j++) /*columns*/
- aij += b[i][j] * c[j];
- a[i] = aij;
- }
- } /*MatrixVector_X*/
-
- void AddVectors(double *a, double *b, double *c, short n)
- {
- /*A:=B+ C A,B,C are n dimensional vectors*/
- short i;
-
- for (i = 0; i <= n - 1; i++)
- a[i] = b[i] + c[i];
- }
-
- void SubtractVectors(double *a, double *b, double *c, short n)
- {
- short i;
-
- for (i = 0; i <= n - 1; i++)
- a[i] = b[i] - c[i];
- }
-
- void ZeroVector(double *a, short n)
- {
- short i;
-
- for (i = 0; i <= n - 1; i++)
- {
- if (a[i] < 0.0)
- a[i] = 0.0;
- }
- }
-
- void VectorInit(double *a, short n)
- {
- short i;
-
- for (i = 0; i <= n - 1; i++)
- a[i] = 0.0;
- }
-
- void DrawVectors(double *a, double *b, short n, short loop,
- short offset, short scale_V)
- {
- short i, v, dv;
-
- for (i = 1; i <= n; i++)
- {
- if (a[i-1] == 0.0)
- { /*look out for zeroed out responses*/
- v = offset;
- dv = 0;
- } else
- {
- v = offset - ceil(a[i-1] * scale_V);
- dv = - ceil(b[i-1] * scale_V);
- }
- if (i == 1)
- SetForeColor (0, 0, 0xffff);
- if (i == 2)
- SetForeColor (0, 0x8888, 0);
- if (i == 3)
- SetForeColor (0xffff, 0, 0);
-
- /* normal condition*/
- MoveTo(loop * (long) scale_H, v);
-
- /*change pen size to indicate which activity is occuring*/
- PenSize(2, 2);
- Line((long) scale_H, dv);
- }
- }
-
- void Vector_X_Scalar(double *a, double s, double *b, short n)
- {
- short i;
-
- for (i = 0; i <= n - 1; i++)
- a[i] = s * b[i];
- }
-
- void Matrix_X_Scalar(double (*a)[maxVectSize], double s,
- double (*b)[maxVectSize], short n)
- {
- short i, j;
-
- for (i = 0; i <= n - 1; i++)
- {
- for (j = 0; j <= n - 1; j++)
- a[i][j] = s * b[i][j];
- }
- }
-
- void Identity(double (*temp)[maxVectSize], short vectSize)
- {
- short i, j;
-
- for (i = 1; i <= vectSize; i++)
- {
- if (i > 1)
- {
- for (j = 0; j <= i - 2; j++)
- {
- temp[i-1][j] = 0.0;
- temp[j][i-1] = 0.0;
- }
- }
- temp[i-1][i-1] = 1.0;
- }
- }
-
- void DynamicSimulation(void)
- {
- short stimuli, loop, maxValue, factor_H;
- Rect writeRect;
- long ticks;
-
- maxValue = myWindow->portRect.right / scale_H;
-
- /*write('enter alpha, personality ');*/
- /*readln(alpha, personality);*/
- alpha = 0.2;
- personality = 1.0;
-
- if (answer == '\n')
- answer = ' ';
-
- vectSize = 3;
- Identity(ident, vectSize);
-
- for (stimuli = 1; stimuli <= vectSize; stimuli++)
- stimulation[stimuli-1] = stimuli * 0.5;
-
- Matrix_X_Scalar(consumation, 0.2, ident, vectSize);
-
- Matrix_X_Scalar(excitation, alpha, ident, vectSize);
-
- for (loop = 0; loop <= vectSize - 1; loop++)
- {
- for (stimuli = 0; stimuli <= vectSize - 1; stimuli++)
- inhibition[loop][stimuli] = 1.0;
- inhibition[loop][loop] = 0.1;
- }
-
- /*zero out initial values of need and response*/
- VectorInit(need, vectSize);
- VectorInit(response, vectSize);
- Matrix_X_Scalar(generalization, personality, ident, vectSize);
-
- SetForeColor (0x9999, 0x9999, 0x9999);
- PenPat (&qd.gray);
- PenSize (1, 1);
- DrawTimeLines ();
- PenNormal ();
- SetForeColor (0, 0, 0);
-
- factor_H = myWindow->portRect.bottom / 10;
-
- MoveTo(10, factor_H);
- DrawString("\pTime -->");
- MoveTo(10, (factor_H * 5));
- DrawString("\pNeeds");
- MoveTo(10, (factor_H * 7));
- DrawString("\pResponses");
-
- /*begin the main loop*/
- for (loop = 1; loop <= maxValue; loop++)
- {
- /*if loop = 250 then*/
- /* ShowDrawing;*/
- MatrixVector_X(tempVct1, generalization, stimulation, vectSize, vectSize);
- MatrixVector_X(consumVector, consumation, response, vectSize, vectSize);
-
- SubtractVectors(deltaNeed, tempVct1, consumVector, vectSize);
-
- MatrixVector_X(tempVct1, inhibition, response, vectSize, vectSize);
- MatrixVector_X(tempVct2, excitation, need, vectSize, vectSize);
-
- SubtractVectors(deltaResponse, tempVct2, tempVct1, vectSize);
-
- DrawVectors(need, deltaNeed, vectSize, loop, 125, scale_V);
-
- AddVectors(need, need, deltaNeed, vectSize);
-
- DrawVectors(response, deltaResponse, vectSize, loop, 300, scale_V);
-
- AddVectors(response, response, deltaResponse, vectSize);
-
- ZeroVector(response, vectSize);
- }
- PenNormal ();
- SetForeColor (0, 0, 0);
- FakeGrowIcon (myWindow, true);
- }
-
- /*the remaining code was added to allow for windows, menus, etc*/
-
- void Message(short dialogNumber)
- {
- DialogPtr theDialog;
- EventRecord myEvent;
- short itemHit;
-
- theDialog = GetNewDialog(dialogNumber, 0L, (WindowPtr)-1L); /*display dialog # */
- ModalDialog(0L, &itemHit); /*show the dialog and wait for a button*/
- DisposDialog(theDialog); /*get rid of the dialog and show the screen*/
- SetPort(myWindow); /*put back the original display*/
- DynamicSimulation ();
- }
-
- void DrawTimeLines ()
- {
- short i, amount, start, end;
-
- amount = myWindow->portRect.right;
- start = 20;
- end = myWindow->portRect.bottom;
-
- for (i = 0; i < amount; i++)
- {
- MoveTo (start, 0);
- LineTo (start, end);
- start += 20;
- }
- }
-
- void FakeGrowIcon (WindowPtr wPtr, Boolean visible)
- {
- short bot, rght;
- Rect r;
- PenState pState;
-
- bot = wPtr->portRect.bottom;
- rght = wPtr->portRect.right;
- SetRect (&growIconRect, rght - 15, bot - 15, rght + 1, bot + 1);
- if (visible)
- {
- GetPenState (&pState);
- EraseRect (&growIconRect);
- FrameRect (&growIconRect);
- SetRect (&r, rght - 10, bot - 10, rght - 1, bot - 1);
- FrameRect (&r);
- SetRect (&r, rght - 12, bot - 12, rght - 5, bot - 5);
- EraseRect (&r);
- FrameRect (&r);
- SetPenState (&pState);
- }
- }
-
- void MyGrowWindow (WindowPtr w, Point p)
- {
- GrafPtr savePort;
- long theResult;
- Rect oldHorizBar;
- Rect r;
-
- GetPort (&savePort);
- SetPort (w);
-
- SetRect (&r, 80, 80, qd.screenBits.bounds.right, qd.screenBits.bounds.bottom);
- theResult = GrowWindow (w, p, &r);
- if (theResult == 0)
- return;
- SizeWindow (w, LoWord (theResult), HiWord (theResult), false);
-
- InvalRect (&w->portRect);
-
- ValidRect (&r);
-
- SetPort (savePort);
- }
-
- short DoCommand (long mResult)
- {
- Str255 name;
- short theMenu, theItem, refNum;
- short AlertId = 1;
-
- theMenu = HiWord(mResult);
- theItem = LoWord(mResult);
- switch (theMenu)
- {
-
- case appleMenu:
- if (theItem == 1) /*throw up message window*/
- {
- Message(1);
- quit = false;
- }
- else
- {
- GetMenuItemText(myMenus[0], theItem, name);
- refNum = OpenDeskAcc(name);
- }
- break;
-
- case fileMenu:
- switch (theItem)
- {
-
- case 1:
- EraseRect (&myWindow->portRect);
- DynamicSimulation ();
- break;
-
- case 2:
- break;
-
- case 3:
- break;
-
- /* AlertNum:=CautionAlert(AlertId,nil);*/
-
- case 5:
- quit = true;
- break;
- }
- break;
-
- case editMenu:
- if (!SystemEdit(theItem-1))
- SysBeep(5);
- break;
- }
- HiliteMenu (0);
- return (1);
- }
-
- short HandleMouseDown (short windowPart, WindowPtr whichWindow, EventRecord *myEvent)
- {
- switch (windowPart)
- {
- case inGoAway:
- CloseWindow (whichWindow);
- break;
-
- case inMenuBar:
- DoCommand (MenuSelect (myEvent->where));
- break;
-
- case inSysWindow:
- SystemClick (myEvent, whichWindow);
- break;
-
- case inDrag:
- break;
-
- case inGrow:
- MyGrowWindow (whichWindow, myEvent->where);
- break;
-
- case inContent:
- if (whichWindow != FrontWindow ())
- SelectWindow (whichWindow);
- break;
- }
- }
-
- void WaitForCommand(void)
- {
- GrafPtr savePort;
- EventRecord myEvent;
- WindowPtr whichWindow;
- short windowPart;
- Rect r;
-
- SystemTask ();
-
- if (GetNextEvent (everyEvent, &myEvent))
- {
- switch (myEvent.what)
- {
- case mouseDown:
- windowPart = FindWindow (myEvent.where, &whichWindow);
- HandleMouseDown (windowPart, whichWindow, &myEvent);
- break;
-
- case keyDown:
- case autoKey:
- DoCommand(MenuKey((char) (myEvent.message & charCodeMask)));
- break;
- }
- }
- }
-
- Boolean ColorCheck (void)
- {
- SysEnvRec theWorld;
- Boolean gotColor = false;
-
- if (theWorld.hasColorQD)
- gotColor = true;
-
- return true;
- }
-
- void main()
- {
- Startup();
- SetUpMenus ();
-
- if (ColorCheck ())
- {
- SetPort ((myWindow = GetNewCWindow (windowID, &wRecord, (WindowPtr)-1L)));
- FakeGrowIcon (myWindow, true);
- while (! quit)
- WaitForCommand(); /*the Main Event Loop*/
- }
- DisposeWindow (myWindow);
- }
-
-